using System;
using System.Collections;
using System.Runtime.Remoting.Channels;

using Pegasus.Diagnostics;

namespace Pegasus.Runtime.Remoting.Channels
{
    /// <summary>
    /// Provides the implementation for the xml server formatter sink provider
    /// </summary>
    public class XmlServerFormatterSinkProvider : IServerFormatterSinkProvider
    {
        // Local Instance Values
        // Local Instance Values
        private IDictionary m_properties;
        private ICollection m_providerData;
        private IServerChannelSinkProvider m_next;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlServerFormatterSinkProvider"/> class.
        /// </summary>
        public XmlServerFormatterSinkProvider()
            : this( null, null )
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlServerFormatterSinkProvider"/> class.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <param name="providerData">The provider data.</param>
        public XmlServerFormatterSinkProvider( IDictionary properties, ICollection providerData )
        {
            // Check Parameters
            // properties can be null
            // providerData can be null

            m_properties = properties;
            m_providerData = providerData;
        }

        /// <summary>
        /// Gets or sets the next sink provider in the channel sink provider chain.
        /// </summary>
        /// <value></value>
        /// <returns>The next sink provider in the channel sink provider chain.</returns>
        /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
        /// <PermissionSet><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure"/></PermissionSet>
        public IServerChannelSinkProvider Next
        {
            get
            {
                return m_next;
            }

            set
            {
                m_next = value;
            }
        }

        /// <summary>
        /// Creates a sink chain.
        /// </summary>
        /// <param name="channel">The channel for which to create the channel sink chain.</param>
        /// <returns>
        /// The first sink of the newly formed channel sink chain, or null, which indicates that this provider will not or cannot provide a connection for this endpoint.
        /// </returns>
        /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
        public IServerChannelSink CreateSink( IChannelReceiver channel )
        {
            // Check Parameters
            ParamCode.AssertNotNull( channel, "channel" );

            // Get the next channel skin in the chain
            IServerChannelSink nextChannelSink = null;
            if( m_next != null )
            {
                nextChannelSink = m_next.CreateSink( channel );
            }

            XmlServerFormatterSink xmlFormatterSink = new XmlServerFormatterSink( nextChannelSink );
            return xmlFormatterSink;
        }

        /// <summary>
        /// Returns the channel data for the channel that the current sink is associated with.
        /// </summary>
        /// <param name="channelData">A <see cref="T:System.Runtime.Remoting.Channels.IChannelDataStore"></see> object in which the channel data is to be returned.</param>
        /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception>
        public void GetChannelData( IChannelDataStore channelData )
        {
			if( m_next != null )
			{
				m_next.GetChannelData( channelData );
			}
		}
    }
}
